package com.hzyc.internetshop.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.hzyc.internetshop.bean.Article;
import com.hzyc.internetshop.bean.Comment;
import com.hzyc.internetshop.bean.Fankui;
import com.hzyc.internetshop.bean.Goods;
import com.hzyc.internetshop.bean.Gys;
import com.hzyc.internetshop.bean.IdList;
import com.hzyc.internetshop.bean.Lianjie;
import com.hzyc.internetshop.bean.Orders;
import com.hzyc.internetshop.bean.Page;
import com.hzyc.internetshop.bean.Question;
import com.hzyc.internetshop.bean.Types;
import com.hzyc.internetshop.service.impl.HoutaiServiceImp;
import com.hzyc.internetshop.service.impl.TypeServiceImp;

@Controller
public class ShopController {
		@Autowired
		private TypeServiceImp ts;
		@Autowired
		private Page page;
		@Autowired
		private HoutaiServiceImp hs;
		
//		锟斤拷锟斤拷锟斤拷
		@RequestMapping("add_type.do")
		public ModelAndView add_type(String type){
			ModelAndView mav=new ModelAndView();
				try {
					boolean bol=false;
					 
					if(!(type==null)){
						 bol=ts.addType(type);					
					}
									
						mav.setViewName("show_type.do");
											
				} catch (Exception e) {
					e.printStackTrace();
				}
			
			return mav;
		}
//		锟斤拷询锟斤拷锟�
		@RequestMapping("show_type.do")
		public ModelAndView show_type(){
			
			ModelAndView mav=new ModelAndView();
			
			try {
				
				List<Types> list= ts.showType();
				mav.addObject("typeList", list);
				mav.setViewName("add_type.jsp");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return mav;
			
		}
//		删锟斤拷锟斤拷锟�
		@RequestMapping("delect_type.do")
		public ModelAndView delete_type(String typeId){
			
			ModelAndView mav=new ModelAndView();
			try {
				
					IdList id=new IdList();	
					List<String> list=new ArrayList<String>();
					String s[]=typeId.split("-");
					for(int i=0;i<s.length;i++){
						list.add(s[i]);
						
					}
					id.setIdList(list);
					boolean bol=false;				 				
				    bol=ts.deletetype(id);												
					mav.setViewName("show_type.do");
										
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		return mav;
		
		}
		
//	          锟斤拷good锟叫诧拷询锟斤拷锟�
		@RequestMapping("/add_goods_showtype.do")
		public ModelAndView show_gtype(Goods good){
			
			ModelAndView mav=new ModelAndView();
			
			try {
				
				List<Types> list= ts.showType();
				List<Gys> list1= ts.showgys();
				mav.addObject("typeList", list);
				mav.addObject("gysList", list1);
				mav.setViewName("add_goods.jsp");
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return mav;
			
		}
		@RequestMapping("addarticle.do")
		public String addarticle(MultipartFile photo,HttpServletResponse response,HttpServletRequest request){
			try {
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				String mainphoto_name = photo.getOriginalFilename();
				String path=request.getSession().getServletContext().getRealPath("/");
				path=path.substring(0,path.length()-1);
				String newPath=path.substring(0,path.lastIndexOf("\\"));
				String finalPath=newPath+"/LGphoto/"+mainphoto_name;
				File file = new File(finalPath);
				photo.transferTo(file);
				Article article=new Article();
				String name=request.getParameter("name");
				String author=request.getParameter("author");
				String bankuai=request.getParameter("bankuai");
				String scan=request.getParameter("scan");
				String context=request.getParameter("editorValue");
				String s1=request.getParameter("s1");
				String s2=request.getParameter("s2");
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				String time=df.format(new Date());
				
				article.setpName(photo.getOriginalFilename());
				article.setFinalPath(finalPath);
				article.setName(name);
				article.setAuthor(author);
				article.setScan(scan);
				article.setContext(context);
				article.setTime(time);
				article.setIsSj("是");
				article.setSentence1(s1);
				article.setSentence2(s2);
				article.setBankuai(bankuai);
				hs.addArticle(article);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "addarticle.jsp";
		}
//		锟斤拷锟斤拷锟狡�
		@RequestMapping("/add_good.do")
		public String add_good(HttpServletResponse response,HttpServletRequest request,Goods good,MultipartFile name_mainphoto,
				MultipartFile photos1,MultipartFile photos2,MultipartFile photos3,MultipartFile photos4){
				
			
			try {
				
				String mainphoto_name = name_mainphoto.getOriginalFilename();
				String path = request.getSession().getServletContext().getRealPath("/");
				path=path.substring(0,path.length()-1);
				String newPath=path.substring(0,path.lastIndexOf("\\"));
				String finalPath=newPath+"/LGphoto/"+mainphoto_name;
			
				File file = new File(finalPath);
				name_mainphoto.transferTo(file);		
				good.setMainphoto(mainphoto_name);
				String photos1_name = photos1.getOriginalFilename();
				if(!photos1_name.equals("")){
					
					String finalPath1=newPath+"/LGphoto/"+photos1_name;
				
					File file1= new File(finalPath1);
					photos1.transferTo(file1);		
					good.setPhoto1(photos1_name);
				}
				
				String photos2_name = photos2.getOriginalFilename();
				if(!photos2_name.equals("")){
				
					String finalPath2=newPath+"/LGphoto/"+photos2_name;
					File file2= new File(finalPath2);
					photos2.transferTo(file2);		
					good.setPhoto2(photos2_name);
					
				}
				String photos3_name = photos3.getOriginalFilename();
				if(!photos3_name.equals("")){
					String finalPath3=newPath+"/LGphoto/"+photos3_name;
					File file3= new File(finalPath3);
					photos3.transferTo(file3);		
					good.setPhoto3(photos3_name);
				}
				
				String photos4_name = photos4.getOriginalFilename();
				if(!photos4_name.equals("")){
					String finalPath4=newPath+"/LGphoto/"+photos4_name;
					File file4= new File(finalPath4);
					photos4.transferTo(file4);		
					good.setPhoto4(photos4_name);
					
				}
				
				boolean bol= ts.addgood(good);
			
				if(bol==true){
					
					return "redirect:add_goods_showtype.do";
					
				}
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "";
			
		}

//  	锟斤拷询锟斤拷品
		@RequestMapping("show_goods.do")
		public ModelAndView show_goods(String name,String gys,HttpServletResponse response,HttpServletRequest request,HttpSession session){
			ModelAndView mav=new ModelAndView();
			try {
				if("".equals(name) || name==null){
					session.setAttribute("name", "");
				}else{
					session.setAttribute("name", name);
				}
				if("".equals(gys) || gys==null){
					session.setAttribute("gys", "");
				}else{
					session.setAttribute("gys", gys);
				}
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				page.startposition();
				page.setName(name);
				page.setGys(gys);
				
				page.setMaxpage(ts.getgoodnum()%page.getPagesize()==0?ts.getgoodnum()%page.getPagesize():(ts.getgoodnum()/page.getPagesize()+1));
				List<Goods> list=ts.showgoods(page);
				List<Gys> list1= ts.showgys();
				mav.addObject("gysList", list1);
				mav.addObject("gList", list);
				mav.addObject("page", page);
				mav.setViewName("showgoods.jsp");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			return mav;
		}
//      锟斤拷询锟斤拷锟斤拷	
		@RequestMapping("show_article.do")
		public ModelAndView show_article(HttpSession session,String first,HttpServletResponse response,HttpServletRequest request,String name){
				ModelAndView mav=new ModelAndView();
				if("".equals(name) || name==null){
					session.setAttribute("name", "");
				}else{
					session.setAttribute("name", name);
				}
				if("".equals(first) || first==null){
					session.setAttribute("first", "");
				}else{
					session.setAttribute("first", first);
				}
			try {
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				
				page.startposition();
				page.setName(name);
				page.setFirst(first);
			
				page.setMaxpage(ts.getarticlenum()%page.getPagesize()==0?ts.getarticlenum()%page.getPagesize():(ts.getarticlenum()/page.getPagesize()+1));
				List<Article> list=ts.showarticles(page);
				
				
				mav.addObject("aList", list);
				mav.addObject("page", page);
				mav.setViewName("showarticles.jsp");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return mav;
			
		}
		
//  	锟斤拷锟斤拷锟斤拷品
		@RequestMapping("/sousuo_show_goods.do")
		public ModelAndView sousuo_show_goods(String name){
			ModelAndView mav=new ModelAndView();
			try {
			
				List<Goods> list=hs.sousuo_goodname(name);
				
				mav.addObject("gList", list);
				
				mav.setViewName("showgoods.jsp");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			return mav;
		}
		
		
//		锟斤拷一页
		@RequestMapping("/pageUp.do")
		public String pageUp(int nowPage,HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
				
				page.uppage(nowPage);
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "show_goods.do?name="+name;
		}
//		锟斤拷一页	
		@RequestMapping("/pageDown.do")
		public String pageDown(int nowPage,HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
				
				page.downpage(nowPage);
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "show_goods.do?name="+name;
		}
//		锟斤拷页
		@RequestMapping("/pageFirst.do")
		public String pageFirst(HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
				
				page.firstpage();
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "show_goods.do?name="+name;
		}
//		尾页
		@RequestMapping("/pageLast.do")
		public String pageLast(HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
			
				page.lastpage();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "show_goods.do?name="+name;
		}
		
		@RequestMapping("delete_good.do")
		public String delete_good(String id){
		
			try {
				
				boolean bol=ts.deletegood(id);
				if(bol==true){
					return "redirect:show_goods.do";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "";
		}
		
		@RequestMapping("getgoodbyid.do")
		public ModelAndView getgoodbyid(String id){
			ModelAndView mav=new ModelAndView();
			try {
				Goods g=ts.getGoodById(id);
				List<Types> list= ts.showType();
				List<Gys> list1= ts.showgys();
				mav.addObject("typeList", list);
				mav.addObject("gysList", list1);
				mav.addObject("good", g);
				mav.setViewName("update_good.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return mav;
		}
		@RequestMapping("getarticlebyid.do")
		public ModelAndView getarticlebyid(String id){
			ModelAndView mav=new ModelAndView();
			try {
				Article a=ts.getArticleById(id);
				List<String> list= hs.loadFirst();
				List<String> list1=hs.loadSecond();
				mav.addObject("firstList", list);
				mav.addObject("secondList", list1);
				mav.addObject("Article", a);
				mav.setViewName("update_article.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return mav;
		}
		
		@RequestMapping("update_good.do")
		public String update_good(HttpServletResponse response,HttpServletRequest request,Goods good,MultipartFile mainphotoName,
				MultipartFile photo1Name,MultipartFile photo2Name,MultipartFile photo3Name,MultipartFile photo4Name){
			
			try {
				String s = good.getMainphoto();	
				String ss = mainphotoName.getOriginalFilename();
				
				if(!s.equals(ss)&&!ss.equals("")){
				
					String finalPath="D:/2锟斤拷锟斤拷锟酵计�"+ss;
					File file = new File(finalPath);
					mainphotoName.transferTo(file);
					good.setMainphoto(ss);
				}
				
				String s1 = good.getPhoto1();	
				String ss1 = photo1Name.getOriginalFilename();
				if(!s1.equals(ss1)&&!ss1.equals("")){
					String finalPath1="D:/2锟斤拷锟斤拷锟酵计�"+ss1;
					File file1 = new File(finalPath1);
					photo1Name.transferTo(file1);
					good.setPhoto1(ss1);
					System.out.println(ss1);
				}
				
				String s2 = good.getPhoto2();	
				String ss2 = photo2Name.getOriginalFilename();
				if(!s2.equals(ss2)&&!ss2.equals("")){
					String finalPath2="D:/2锟斤拷锟斤拷锟酵计�"+ss2;
					File file2 = new File(finalPath2);
					photo2Name.transferTo(file2);
					good.setPhoto2(ss2);
				}
				
				
				String s3 = good.getPhoto3();	
				String ss3 = photo3Name.getOriginalFilename();
				if(!s3.equals(ss3)&&!ss3.equals("")){
					String finalPath3="D:/2锟斤拷锟斤拷锟酵计�"+ss3;
					File file3 = new File(finalPath3);
					photo3Name.transferTo(file3);
					good.setPhoto3(ss3);
				}
				
				String s4 = good.getPhoto4();	
				String ss4 = photo4Name.getOriginalFilename();
				if(!s4.equals(ss4)&&!ss4.equals("")){
					String finalPath4="D:/2锟斤拷锟斤拷锟酵计�"+ss4;
					File file4 = new File(finalPath4);
					photo4Name.transferTo(file4);
					good.setPhoto4(ss4);
				}
				ts.updategood(good);
				return "redirect:show_goods.do";
			} catch (Exception e) {
				e.printStackTrace();
			}
		
			
			return "";
		}
	
	@RequestMapping("LoadFirst.do")
	public void LoadFirst(HttpServletRequest request, HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			List<String> list=hs.loadFirst();
			String info = "";
			for(int i=0;i<list.size();i++){
				info=info+"@@@"+list.get(i);
			}
			out.print(info);
			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping("LoadSecond.do")
	public void LoadSecond(HttpServletRequest request, HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			List<String> list=hs.loadSecond();
			String info = "";
			for(int i=0;i<list.size();i++){
				info=info+"@@@"+list.get(i);
			}
			out.print(info);
			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
//	锟斤拷一页
	@RequestMapping("/pageUp11.do")
	public String pageUp11(int nowPage,HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.uppage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_article.do?name="+name;
	}
//	锟斤拷一页	
	@RequestMapping("/pageDown11.do")
	public String pageDown11(int nowPage,HttpSession session){
		String name=session.getAttribute("name").toString();	
		try {
			
			page.downpage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_article.do?name="+name;
	}
//	锟斤拷页
	@RequestMapping("/pageFirst11.do")
	public String pageFirst11(HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.firstpage();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_article.do?name="+name;
	}
//	尾页
	@RequestMapping("/pageLast11.do")
	public String pageLast11(HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
		
			page.lastpage();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_article.do?name="+name;
	}
	
	@RequestMapping("update_article.do")
	public String update_article(MultipartFile photo,HttpServletResponse response,HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			Article article=new Article();
			String mainphoto_name = photo.getOriginalFilename();
			if("".equals(mainphoto_name) || mainphoto_name==null){
				article.setpName(request.getParameter("oldname"));
				article.setFinalPath(request.getParameter("oldpath"));
			}else{
				article.setpName(mainphoto_name);
				String path=request.getSession().getServletContext().getRealPath("/");
				path=path.substring(0,path.length()-1);
				String newPath=path.substring(0,path.lastIndexOf("\\"));
				String finalPath=newPath+"/LGphoto/"+mainphoto_name;
				File file = new File(finalPath);
				photo.transferTo(file);
				article.setFinalPath(finalPath);
			}
			int id=Integer.parseInt(request.getParameter("id"));
			String name=request.getParameter("name").trim();
			String author=request.getParameter("author").trim();
			String bankuai=request.getParameter("bankuai").trim();
			String scan=request.getParameter("scan").trim();
			String context=request.getParameter("editorValue").trim();
			String s1=request.getParameter("s1").trim();
			String s2=request.getParameter("s2").trim();
			String isSj=request.getParameter("isSj").trim();
			
			
			article.setId(id);
			article.setName(name);
			article.setAuthor(author);
			article.setScan(scan);
			article.setContext(context);
			article.setIsSj(isSj);
			article.setSentence1(s1);
			article.setSentence2(s2);
			article.setBankuai(bankuai);
			hs.updateArticle(article);
				
			return "redirect:show_article.do";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	@RequestMapping("delete_article.do")
	public String delete_article(String id){
	
		try {
			
			boolean bol=ts.deletearticle(id);
			if(bol==true){
				return "redirect:show_article.do";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
//	添加供应商
	@RequestMapping("add_gys.do")
	public ModelAndView add_gys(String name){
		ModelAndView mav=new ModelAndView();
			try {
				boolean bol=false;
				 
				if(!(name==null)){
					 bol=ts.addgys(name);					
				}
								
					mav.setViewName("show_gys.do");
										
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		return mav;
	}
	@RequestMapping("show_gys.do")
	public ModelAndView show_gys(){
		
		ModelAndView mav=new ModelAndView();
		
		try {
			
			List<Gys> list= ts.showgys();
			mav.addObject("gysList", list);
			mav.setViewName("add_gys.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
		
	}
	
	@RequestMapping("delect_gys.do")
	public ModelAndView delete_gys(String gysId){
		
		ModelAndView mav=new ModelAndView();
		try {
			
				IdList id=new IdList();	
				List<String> list=new ArrayList<String>();
				String s[]=gysId.split("-");
				for(int i=0;i<s.length;i++){
					list.add(s[i]);
					
				}
				id.setIdList(list);
				boolean bol=false;				 				
			    bol=ts.deletegys(id);												
				mav.setViewName("show_gys.do");
									
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	return mav;
	
	}
	@RequestMapping("lyb.do")
	public ModelAndView show_fk(){
		
		ModelAndView mav=new ModelAndView();
		
		try {
			
			List<Fankui> list= ts.showFk();
			mav.addObject("fkList", list);
			mav.setViewName("show_fankui.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
		
	}
	@RequestMapping("add_help.do")
	public String add_help(MultipartFile photo,HttpServletResponse response,Question q,HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			ts.addHelp(q);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "addhelp.jsp";
	}
	@RequestMapping("show_help.do")
	public ModelAndView show_help(HttpSession session,String name,HttpServletResponse response,HttpServletRequest request){
			ModelAndView mav=new ModelAndView();
			if("".equals(name) || name==null){
				session.setAttribute("name", "");
			}else{
				session.setAttribute("name", name);
			}
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			
			page.startposition();
			page.setName(name);
		
			page.setMaxpage(ts.gethelpnum()%page.getPagesize()==0?ts.gethelpnum()%page.getPagesize():(ts.gethelpnum()/page.getPagesize()+1));
			List<Question> list=ts.showhelp(page);
			
			
			mav.addObject("hList", list);
			mav.addObject("page", page);
			mav.setViewName("showhelp.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
		
	}
	@RequestMapping("/pageUp123.do")
	public String pageUp123(int nowPage,HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.uppage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_help.do?name="+name;
	}
//	锟斤拷一页	
	@RequestMapping("/pageDown123.do")
	public String pageDown123(int nowPage,HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.downpage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_help.do?name="+name;
	}
//	锟斤拷页
	@RequestMapping("/pageFirst123.do")
	public String pageFirst123(HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.firstpage();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_help.do?name="+name;
	}
//	尾页
	@RequestMapping("/pageLast123.do")
	public String pageLast123(HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
		
			page.lastpage();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_help.do?name="+name;
	}
	@RequestMapping("gethelpbyid.do")
	public ModelAndView gethelpbyid(String id){
		ModelAndView mav=new ModelAndView();
		try {
			Question q=ts.getHelpById(id);
			mav.addObject("Question", q);
			mav.setViewName("update_help.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	@RequestMapping("update_help.do")
	public String update_help(HttpServletResponse response,HttpServletRequest request,Question q){
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			ts.updatehelp(q);
			return "redirect:show_help.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
		return "";
	}
	@RequestMapping("delete_help.do")
	public String delete_help(String id){
	
		try {
			
			boolean bol=ts.deletehelp(id);
			if(bol==true){
				return "redirect:show_help.do";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	@RequestMapping("getpl.do")
	public ModelAndView getpl(String id){
		
		ModelAndView mav=new ModelAndView();
		
		try {
			
			List<Comment> list= ts.getCommentById(id);
			mav.addObject("cList", list);
			mav.setViewName("show_comment.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
		
	}
	
	@RequestMapping("/add_lj.do")
	public String add_lj(HttpServletResponse response,HttpServletRequest request,Lianjie lj,MultipartFile name_mainphoto){
		try {
			
			String mainphoto_name = name_mainphoto.getOriginalFilename();
			String path = request.getSession().getServletContext().getRealPath("/");
			path=path.substring(0,path.length()-1);
			String newPath=path.substring(0,path.lastIndexOf("\\"));
			String finalPath=newPath+"/LGphoto/"+mainphoto_name;
		
			File file = new File(finalPath);
			name_mainphoto.transferTo(file);		
			lj.setPhoto(finalPath);
			lj.setPname(mainphoto_name);
			boolean bol= ts.addlj(lj);
		
			if(bol==true){
				
				return "redirect:add_lj.jsp";
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		
	}
	
	@RequestMapping("show_lj.do")
	public ModelAndView show_lj(HttpSession session,String title,HttpServletResponse response,HttpServletRequest request){
			ModelAndView mav=new ModelAndView();
			if("".equals(title) || title==null){
				session.setAttribute("title", "");
			}else{
				session.setAttribute("title", title);
			}
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			
			page.startposition();
			page.setName(title);
		
			page.setMaxpage(ts.getljnum()%page.getPagesize()==0?ts.getljnum()%page.getPagesize():(ts.getljnum()/page.getPagesize()+1));
			List<Lianjie> list=ts.showlj(page);
			
			
			mav.addObject("lList", list);
			mav.addObject("page", page);
			mav.setViewName("show_lj.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;	
	}
	
	@RequestMapping("/pageUp1234.do")
	public String pageUp1234(int nowPage,HttpSession session){
		String title=session.getAttribute("title").toString();
		try {
			
			page.uppage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_lj.do?title="+title;
	}
//	锟斤拷一页	
	@RequestMapping("/pageDown1234.do")
	public String pageDown1234(int nowPage,HttpSession session){
		String title=session.getAttribute("title").toString();
		try {
			
			page.downpage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_lj.do?title="+title;
	}
//	锟斤拷页
	@RequestMapping("/pageFirst1234.do")
	public String pageFirst1234(HttpSession session){
		String title=session.getAttribute("title").toString();
		try {
			
			page.firstpage();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_lj.do?title="+title;
	}
//	尾页
	@RequestMapping("/pageLast1234.do")
	public String pageLast1234(HttpSession session){
		String title=session.getAttribute("title").toString();
		try {
		
			page.lastpage();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show_lj.do?title="+title;
	}
	@RequestMapping("getljbyid.do")
	public ModelAndView getljbyid(String id){
		ModelAndView mav=new ModelAndView();
		try {
			Lianjie lj=ts.getLjById(id);
			mav.addObject("Lianjie", lj);
			mav.setViewName("update_lj.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	@RequestMapping("update_lj.do")
	public String update_lj(HttpServletResponse response,HttpServletRequest request,Lianjie lj,MultipartFile mainphotoName){

		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			String mainphoto_name = mainphotoName.getOriginalFilename();
			if("".equals(mainphoto_name) || mainphoto_name==null){
				lj.setPhoto(request.getParameter("oldpath"));
				lj.setPname(request.getParameter("oldname"));
			}else{
				lj.setPname(mainphoto_name);
				String path=request.getSession().getServletContext().getRealPath("/");
				path=path.substring(0,path.length()-1);
				String newPath=path.substring(0,path.lastIndexOf("\\"));
				String photo=newPath+"/LGphoto/"+mainphoto_name;
				File file = new File(photo);
				mainphotoName.transferTo(file);
				lj.setPhoto(photo);
			}
			
			ts.updatelj(lj);
			return "redirect:show_lj.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	@RequestMapping("delete_lj.do")
	public String delete_lj(String id){
	
		try {
			
			boolean bol=ts.deletelj(id);
			if(bol==true){
				return "redirect:show_lj.do";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	@RequestMapping("getOrderById.do")
	public ModelAndView getOrderById(String id,HttpServletResponse response,HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		try {
			Orders o=ts.getOrderById(id);
			mav.addObject("orders", o);
			request.setAttribute("sex", o.getSex());
			mav.setViewName("update_order.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	@RequestMapping("update_order.do")
	public String update_order(HttpServletResponse response,HttpServletRequest request,Orders o){
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			ts.updateorder(o);
			return "redirect:select_orders.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
		return "";
	}
	@RequestMapping("qrfh.do")
	public String qrfh(HttpServletResponse response,HttpServletRequest request,String id){
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			ts.updatefh(id);
			return "redirect:select_orders.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
		return "";
	}
}
		
